/**
 * Copyright (C), 2019-2023,
 * FileName: XgkService
 * Author:   fanxquer
 * Date:     2023/7/6 15:47
 * Description: Xgk三方接口服务
 * History:
 * <author>          <time>          <version>          <desc>
 * 邱富康           2023/7/6           1.0
 */
package com.hsteach.service.third;

import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.hsteach.common.core.domain.model.ClassDuty;
import com.hsteach.common.core.domain.model.Eclass;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.exception.XgkTokenException;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.common.utils.sign.Md5Utils;
import com.hsteach.framework.feign.client.xgk.XgkClient;
import com.hsteach.framework.feign.client.xgk.dto.*;
import com.hsteach.framework.feign.client.xgk.dto.ret.*;
import com.hsteach.framework.feign.ClientConstant;
import com.hsteach.framework.feign.client.xgk.dto.ret.preschool.PreschoolGetParentsRet;
import com.hsteach.framework.feign.client.xgk.dto.ret.preschool.PreschoolGetTeacherRet;
import com.hsteach.util.AESUtil;
import com.hsteach.web.controller.common.dto.GetPersonListByOrgListAndPersonNameDto;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈Xgk三方接口服务〉
 *
 * @author Qiu Fukang
 * @create 2023/7/6
 * @since 1.0.0
 */
@Service
public class XgkService {

    @Autowired
    private XgkClient xgkClient;

    /**
     * 查询机构所有用户 有缓存
     * @return
     */
    @Cacheable(cacheNames = "allusers", key = "#orgCode")
    public List<GetPersonByDatabaseRet> getAllUsers(String orgCode) {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkListResp<GetPersonByDatabaseRet> resp = xgkClient.getPersonByDatabase(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询人员额外信息
     * @param personId
     * @return
     */
    @Cacheable(cacheNames = "getPersonRelations", key = "#personId")
    public GetPersonRelationsRet getPersonRelations(String personId) {
        GetPersonRelationsEntity entity = new GetPersonRelationsEntity();
        entity.setPersonId(personId);
        XgkReq<GetPersonRelationsEntity> req = new XgkReq<>();
        req.setData(entity);
        XgkDataResp<GetPersonRelationsRet> resp = xgkClient.getPersonRelations(null, null, req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询机构下所有教职工 根据当前用户id缓存
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "teachers", key = "#id")
    public List<GetTeacherRet> getTeachers(String id) {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkListResp<GetTeacherRet> resp = xgkClient.getTeachers(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询班级树
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "getSchoolPartTree", key = "#id")
    public GetSchoolPartTreeRet getSchoolPartTree(String id) {
        XgkDataResp<GetSchoolPartTreeRet> resp = xgkClient.getSchoolPartTree(new XgkReq());
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 根据学校id查询班级树
     * @param
     * @return
     */
    @Cacheable(cacheNames = "getClassTreeBySchoolId", key = "#schoolId")
    public GetSchoolPartTreeRet getClassTreeBySchoolId(String schoolId) {
        XgkDataResp<GetSchoolPartTreeRet> resp = xgkClient.getClassTreeBySchoolId(schoolId,new XgkReq());
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取班级树（根据当前用户权限）
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "getSchoolPartTreeAuth", key = "#id")
    public GetSchoolPartTreeRet getSchoolPartTreeAuth(String id) {
        XgkDataResp<GetSchoolPartTreeRet> resp = xgkClient.getSchoolPartTreeAuth(new XgkReq());
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    @Cacheable(cacheNames = "getAllGrade", key = "#orgCode")
    public List<GetSchoolPartTreeRet> getAllGrade(String orgCode) {
        GetSchoolPartTreeRet schoolPartTree = getSchoolPartTree(orgCode);
        // 查找类型为年级的
        List<GetSchoolPartTreeRet> tree = getTree(schoolPartTree, 2);
        return tree;
    }

    @Cacheable(cacheNames = "getAllClass", key = "#orgCode")
    public List<GetSchoolPartTreeRet> getAllClass(String orgCode) {
        GetSchoolPartTreeRet schoolPartTree = getSchoolPartTree(orgCode);
        // 查找类型为班级的
        List<GetSchoolPartTreeRet> tree = getTree(schoolPartTree, 3);
        return tree;
    }

    public GetSchoolPartTreeRet getClassByGrade(String orgCode, String unitId) {
        GetSchoolPartTreeRet schoolPartTree = getSchoolPartTree(orgCode);
        // 查找类型为班级的
        GetSchoolPartTreeRet byUnitId = getByUnitId(schoolPartTree, unitId);
        return byUnitId;
    }

    /**
     * 根据年级ids查询所有班级对象
     * @param unitIds
     * @param orgCode
     * @return
     */
    public List<GetSchoolPartTreeRet> getClassesByGradeIds(List<String> unitIds, String orgCode) {
        GetSchoolPartTreeRet schoolPartTree = getSchoolPartTree(orgCode);
        // 查找类型为班级的
        List<GetSchoolPartTreeRet> childList = getChildList(schoolPartTree, unitIds);
        return childList;
    }

    /**
     * 查找树枝
     * @param schoolPartTree
     * @param unitIds
     * @return
     */
    private List<GetSchoolPartTreeRet> getChildList(GetSchoolPartTreeRet schoolPartTree, List<String> unitIds) {
        List<GetSchoolPartTreeRet> ret = Lists.newArrayList();
        for (GetSchoolPartTreeRet tree : schoolPartTree.getChildren()) {
            if (unitIds.contains(tree.getUnitId())) {
                ret.addAll(tree.getChildren());
            } else {
                ret.addAll(getChildList(tree, unitIds));
            }
        }

        return ret;
    }

    /**
     * 根据unitId查找树枝
     * @param schoolPartTree
     * @param unitId
     * @return
     */
    private GetSchoolPartTreeRet getByUnitId(GetSchoolPartTreeRet schoolPartTree, String unitId) {
        if (schoolPartTree.getUnitId().equals(unitId)) {
            return schoolPartTree;
        } else if (StringUtils.isEmpty(unitId)) {
            return null;
        } else {
            for (GetSchoolPartTreeRet tree : schoolPartTree.getChildren()) {
                if (tree.getUnitId().equals(unitId)) {
                    return tree;
                } else {
                    GetSchoolPartTreeRet byUnitId = getByUnitId(tree, unitId);
                    if (byUnitId != null) {
                        return byUnitId;
                    }
                }
            }
        }

        return null;
    }

    /**
     * 查找某种类型的树枝，flat为一个list
     * @param schoolPartTree
     * @param unitType
     * @return
     */
    private List<GetSchoolPartTreeRet> getTree(GetSchoolPartTreeRet schoolPartTree, Integer unitType) {
        if (schoolPartTree.getUnitType().equals(unitType)) {
            return Lists.newArrayList(schoolPartTree);
        } else if (unitType == null) {
            return Lists.newArrayList(schoolPartTree);
        } else {
            List<GetSchoolPartTreeRet> list = Lists.newArrayList();
            for (GetSchoolPartTreeRet tree : schoolPartTree.getChildren()) {
                if (tree.getUnitType().equals(unitType)) {
                    list.add(tree);
                } else {
                    list.addAll(getTree(tree, unitType));
                }
            }
            return list;
        }
    }

    /**
     * 根据班级id查询学生列表
     * @param ids
     * @return
     */
    @Cacheable(cacheNames = "getStudentByClassIds", key = "#ids")
    public List<PersonInfoRet> getStudentByClassIds(List<String> ids) {
        XgkReq<GetStudentListByGradeAndClassEntity> req = new XgkReq();
        GetStudentListByGradeAndClassEntity data = new GetStudentListByGradeAndClassEntity();
        data.setClassIds(ids);
        req.setData(data);
        XgkListResp<PersonInfoRet> resp = xgkClient.getStudentListByGradeAndClass(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 通过班级/年级查询学生列表
     * @return
     */
    public List<PersonInfoRet> getStudentListByGradeAndClass(GetStudentListByGradeAndClassEntity entity) {
        XgkReq<GetStudentListByGradeAndClassEntity> req = new XgkReq();
        req.setData(entity);
        XgkListResp<PersonInfoRet> resp = xgkClient.getStudentListByGradeAndClass(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 通过班级或年级查询教师
     * @return
     */
    public List<PersonInfoRet> getTeacherListByGradeAndClass(GetStudentListByGradeAndClassEntity entity) {
        XgkReq<GetStudentListByGradeAndClassEntity> req = new XgkReq();
        req.setData(entity);
        XgkListResp<PersonInfoRet> resp = xgkClient.getTeacherListByGradeAndClass(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 通过班级或年级查询教师
     * @return
     */
    public List<GetTeacherRet> getTeacherListByGradeInfo(GetTeacherInfoEntity entity) {
        XgkReq<GetTeacherInfoEntity> req = new XgkReq();
        req.setData(entity);
        XgkListResp<GetTeacherRet> resp = xgkClient.getClassTeacher(SecurityUtils.getAuthInfo().getXgkToken(),SecurityUtils.getAuthInfo().getOrgCode(),req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 通过班级或年级查询教师
     * @return
     */
    public List<GetTeacherRet> getTeachersByClassId(String classId) {
        XgkReq<GetTeacherInfoEntity> req = new XgkReq();
        GetTeacherInfoEntity entity = new GetTeacherInfoEntity();
        entity.setClassId(classId);
        req.setData(entity);
        XgkListResp<GetTeacherRet> resp = xgkClient.getTeachersByClassId(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }



    /**
     * 根据personId查询学生
     * @param personIds
     * @return
     */
    public List<PersonInfoRet> getStudentByPersonIds(List<String> personIds) {
        XgkReq<GetStudentListByPersonIdsEntity> req = new XgkReq();
        GetStudentListByPersonIdsEntity data = new GetStudentListByPersonIdsEntity();
        data.setPersonIds(personIds);
        req.setData(data);
        XgkListResp<PersonInfoRet> resp = xgkClient.getPersonInfoList(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询执教班级信息，仅教师可用
     * @return
     */
    public List<GetClassListByIdsRet> listClasses() {
        List<String> classIds = SecurityUtils.getAuthInfo().getEclasses().stream().map(Eclass::getEclassId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(classIds)) {
            return Lists.newArrayList();
        }
        XgkReq<GetClassListByIdsEntity> req = new XgkReq<>();
        GetClassListByIdsEntity data = new GetClassListByIdsEntity();
        data.setClassIdList(classIds);
        req.setData(data);
        XgkListResp<GetClassListByIdsRet> resp = xgkClient.getClassListByIds(req);
        if (isSuccess(resp)) {
            List<GetClassListByIdsRet> classes = resp.getData();
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询家长通讯录，根据当前用户
     * @param userId
     * @return
     */
    @Cacheable(cacheNames = "parentsContact", key = "#userId")
    public List<GetParentsRet> getParent(String userId) {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkListResp<GetParentsRet> resp = xgkClient.getParents(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询当前账号能看到的班级及学生
     * @param userId
     * @return
     */
    @Cacheable(cacheNames = "getClazzWithStudents", key = "#userId")
    public List<GetClazzWithStudentsRet> getClazzWithStudents(String userId) {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkListResp<GetClazzWithStudentsRet> resp = xgkClient.getClazzWithStudents(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    private boolean isSuccess(XgkResp resp) {
        if (StringUtils.isNotEmpty(resp.getMessage()) && resp.getMessage().indexOf("用户令牌己更新") != -1) {
            throw new XgkTokenException(resp.getMessage());
        }
        if (ClientConstant.XGK_SUCCESS.equals(resp.getCode())) {
            return true;
        }
        return false;
    }

    private boolean isSuccess2(XgkResp resp) {
        if (StringUtils.isNotEmpty(resp.getMessage()) && resp.getMessage().indexOf("用户令牌己更新") != -1) {
            throw new XgkTokenException(resp.getMessage());
        }
        if (ClientConstant.XGK_ISCMS_SUCCESS.equals(resp.getCode())) {
            return true;
        }
        return false;
    }


    /**
     * 根据buildingId查询子树信息
     * @param buildingId
     * @return
     */
    public GetPlaceChildrenTreeRet getPlaceChildrenTree(String buildingId){
        XgkReq<GetPlaceChildrenTreeEntity> req = new XgkReq();
        GetPlaceChildrenTreeEntity data = new GetPlaceChildrenTreeEntity();
        data.setBuildingId(buildingId);
        req.setData(data);
        XgkDataResp<GetPlaceChildrenTreeRet> resp = xgkClient.getPlaceChildrenTree(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取场所树
     * @param
     * @return
     */
    public GetPlaceChildrenTreeRet getPlaceTree() {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkDataResp<GetPlaceChildrenTreeRet> resp = xgkClient.getPlaceTree(SecurityUtils.getAuthInfo().getOrgCode(),req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取场所树
     * @param
     * @return
     */
    public GetPlaceChildrenTreeRet getPlaceTreeNotNull() {
        GetPlaceChildrenTreeRet placeTree = getPlaceTree();
        return null;
    }

    /**
     * 根据（校区、楼栋、楼层，均可空）获取教室列表
     * @param
     * @return
     */
    @Cacheable(cacheNames = "parentsContact", key = "#buildingId")
    public List<GetClassRoomListByPlaceIdRet> getClassRoomListByPlaceId(Integer type,String buildingId) {
        XgkReq<GetClassRoomListByPlaceIdEntity> req = new XgkReq();
        GetClassRoomListByPlaceIdEntity data = new GetClassRoomListByPlaceIdEntity();
        data.setType(type);
        data.setBuildingId(buildingId);
        req.setData(data);
        XgkListResp<GetClassRoomListByPlaceIdRet> resp = xgkClient.getClassRoomListByPlaceId(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取部门组织树
     * @param orgCode
     * @param type
     * @return
     */
    public GetOrgTreeListByPlaceIdRet getAdmOrgTreed(String orgCode,String type){
        XgkReq<GetOrgTreeListByPlaceIdEntity> req = new XgkReq();
        GetOrgTreeListByPlaceIdEntity data = new GetOrgTreeListByPlaceIdEntity();
        data.setOrgId(orgCode);
        data.setType(type);
        req.setData(data);
        XgkDataResp<GetOrgTreeListByPlaceIdRet> resp = xgkClient.getAdmOrgTreed(SecurityUtils.getAuthInfo().getOrgCode(),req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }


    /**
     * 获取部门组织下人员列表
     * @param orgCode
     * @param type
     * @return
     */
    public GetDataListVo getAdmBraPerList(String orgCode, String type,String name) {
        XgkReq<GetOrgTreeListByPlaceIdEntity> req = new XgkReq();
        GetOrgTreeListByPlaceIdEntity data = new GetOrgTreeListByPlaceIdEntity();
        data.setOrgId(orgCode);
        data.setType(type);
        data.setName(name);
        req.setData(data);
        XgkDataResp<GetDataListVo<GetAdmBraPerListRet>> resp = xgkClient.getAdmBraPerList(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 通过机构列表和人员名称查询人员列表(分页)
     * @param dto
     * @return
     */
    public GetDataListVo getPersonListByOrganListWithPages(GetPersonListByOrgListAndPersonNameDto dto) {
        XgkReq<GetPersonListByOrgListAndPersonNameEntity> req = new XgkReq();
        GetPersonListByOrgListAndPersonNameEntity data = new GetPersonListByOrgListAndPersonNameEntity();
        data.setOrganIdList(dto.getOrgCodeList());
        data.setName(dto.getName());
        data.setCurrent(dto.getCurrent());
        data.setPageSize(dto.getPageSize());
        req.setData(data);
        XgkDataResp<GetDataListVo<GetPersonListByOrgListAndPersonNameRet>> resp = xgkClient.getPersonListByOrganListWithPages(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 根据场所id 和 日期 获取课表信息
     * @param buildingId
     * @param date
     * @return
     */
    public List<QuerySinglePlaceTableRet> querySinglePlaceTable(String buildingId, String date) {
        XgkReq<QuerySinglePlaceTableEntity> req = new XgkReq();
        QuerySinglePlaceTableEntity data = new QuerySinglePlaceTableEntity();
        data.setPlaceId(buildingId);
        data.setSchoolroomDate(date);
        req.setData(data);
        XgkListResp<QuerySinglePlaceTableRet> resp = xgkClient.querySinglePlaceTable(req);
        if (isSuccess2(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }

    }

    /**
     * 获取教学机构树
     * @return
     */
    public GetTeaOrgTreeRet getTeaOrgTree() {
        XgkReq<JSONObject> req = new XgkReq();
        req.setData(new JSONObject());
        XgkDataResp<GetTeaOrgTreeRet> resp = xgkClient.getTeaOrgTree(SecurityUtils.getAuthInfo().getOrgCode(),req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取教学机构人员信息
     * @param getOrganPersonListEntity
     * @return
     */
    public GetDataListVo getOrganPersonList(GetOrganPersonListEntity getOrganPersonListEntity) {
        XgkReq<GetOrganPersonListEntity> req = new XgkReq();
        req.setData(getOrganPersonListEntity);
        XgkDataResp<GetDataListVo<GetOrganPersonListRet>> resp = xgkClient.getOrganPersonList(SecurityUtils.getAuthInfo().getOrgCode(),req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 根据spyCode查询拥有当前菜单权限的所有人员
     * @param entity
     * @return
     */
    public List<GetPersonBySpyCodeRet> getPersonBySpyCode(GetPersonBySpyCodeEntity entity) {
        XgkReq<GetPersonBySpyCodeEntity> req = new XgkReq();
        req.setData(entity);
        XgkListResp<GetPersonBySpyCodeRet> resp = xgkClient.getPersonBySpyCode(req);
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取学年列表
     * @param orgCode
     * @return
     */
    @Cacheable(cacheNames = "getYearList", key = "#orgCode")
    public List<GetYearListRet> getYearList(String orgCode) {
        XgkListResp<GetYearListRet> resp = xgkClient.getYearList(new XgkReq());
        if (isSuccess(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 根据证件号码查询学生列表
     * @param
     * @return
     */
    public List<StudentRet> getStudentBySchoolId(String orgCode,String idCardNo) {
        JSONObject req = new JSONObject();
        JSONObject data = new JSONObject();
        String[] idCardNos = idCardNo != null ? new String[]{idCardNo} : new String[]{};
        data.put("idCardNos", idCardNos);
        req.put("data", data);
        XgkListResp<StudentRet> resp = xgkClient.getStudentBySchoolId(orgCode,req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 根据证学籍号查询学生列表
     * @param
     * @return
     */
    public List<StudentRet> getStudentByStuCode(String orgCode,String stuCode) {
        JSONObject req = new JSONObject();
        JSONObject data = new JSONObject();
        data.put("stuNos", new String[]{stuCode});
        req.put("data", data);
        XgkListResp<StudentRet> resp = xgkClient.getStudentBySchoolId(orgCode,req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 查询所有学校
     * @param
     * @return
     */
    public List<SchoolRet> getSchool() {
        JSONObject req = new JSONObject();
        XgkListResp<SchoolRet> resp = xgkClient.getSchool(req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }


    /**
     * 幼教老师通讯录
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "preschoolTeachers", key = "#id")
    public List<PreschoolGetTeacherRet> getPreschoolTeachers(String id) {
        XgkListResp<PreschoolGetTeacherRet> resp = xgkClient.getPreschoolTeachers(SecurityUtils.getAuthInfo().getAccessToken());
        if(resp == null) {
            return new ArrayList<>();
        }
        if (isSuccess2(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 幼教家长通讯录
     * @param id
     * @return
     */
    @Cacheable(cacheNames = "preschoolParents", key = "#id")
    public List<PreschoolGetParentsRet> getParentList(String id) {
        XgkListResp<PreschoolGetParentsRet> resp = xgkClient.getParentList(SecurityUtils.getAuthInfo().getAccessToken());
        if(resp == null) {
            return new ArrayList<>();
        }
        if (isSuccess2(resp)) {
            return resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }

    /**
     * 获取任职班主任副班主任的班级id
     * @param personId
     * @return
     */
    public List<String> mainClassId(String personId) {
        return getPersonRelations(personId).getJobList().stream().filter(e -> {
            if (e.getJobName().equals("班主任") || e.getJobName().equals("副班主任")) {
                return true;
            }
            return false;
        }).map(ClassDuty::getClassId).collect(Collectors.toList());
    }

    /**
     * 获取指定学校的基础数据：学校名称，学生数量
     * @param orgCode 学校Code
     * @return
     */
    public XgkListResp<SchoolRet> getStudentCountsByOrgCode(String orgCode) {

        JSONObject payload = new JSONObject();
        JSONObject data = new JSONObject();
        ArrayList<String> orgCodes = new ArrayList<>();
        orgCodes.add(orgCode);
        data.put("orgCodes", orgCodes);
        payload.put("data", data);
        return xgkClient.getSchool(payload);
    }

    /**
     * 根据personIds查询人员信息
     * @param personIds
     * @return
     */
    public List<PersonRet> getPersonInfoList(List<String> personIds) {
        XgkReq<GetStudentListByPersonIdsEntity> req = new XgkReq();
        GetStudentListByPersonIdsEntity data = new GetStudentListByPersonIdsEntity();
        data.setPersonIds(personIds);
        req.setData(data);
        String time = String.valueOf(new Date().getTime());
        String appToken = AESUtil.encrypt("JF-ISWCC-WEB00003A32774441869306A38D38CCDA41" + time, Md5Utils.encipher16(time));
        XgkListResp<PersonRet> resp = xgkClient.getPersonListByPersonIds("JF-ISWCC-WEB", appToken,time,req);
        if (isSuccess(resp)) {
            return  resp.getData();
        } else {
            throw new ServiceException("Xgk api error: " + resp.getMessage());
        }
    }}
